package com.daikun.common.enums;

import com.daikun.common.iface.BaseResultIface;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;

/**
 * 统一错误 code 码收集，当前项目的所有 code 码只能在此处进行维护
 * @author earl created 2025-07-03
 * @version init 0.0.1
 */
@Getter
@RequiredArgsConstructor
public enum BaseResultEnum implements BaseResultIface {
    SUCCESS(200, "success", "成功"),
    PARAM_ERROR(400, "param error !", "参数校验失败"),
    PARAM_FAIL(405, "param cannot be empty !", "参数错误"),
    LOCK_FAIL(406, "lock fail !", "加锁失败"),
    SYSTEM_ERROR(500, "system error !", "系统错误"),
    SERVICE_CALL_FAIL(501, "Tripartite service call fail !", "服务内部调用其他的三方服务失败"),
    UN_KNOW_ERROR(502, "system error !", "业务上预防兜底的错误"),
    CONFIG_ERROR(503, "config error !", "自定义配置信息错误"),
    DICT_CONFIG_ERROR(504, "dict config error !", "字典配置错误"),
    SERVER_DATA_ERROR(505, "server data error !", "服务内部数据异常"),

    SECURITY_ENC_LIMIT(510, "enc number overrun !", "加密数据数量超过限制"),
    SECURITY_DEC_LIMIT(511, "dec number overrun !", "解密数据数量超过限制"),
    SECURITY_FAIL_01(512, "enc or dec without result !", "加解密请求失败，未获取到返回值"),
    SECURITY_FAIL_02(513, "enc or dec result not json !", "加解密请求失败，响应非 json 结构体"),
    SECURITY_FAIL_03(514, "enc or dec result code not 0 !", "加解密请求失败，响应状态码非 0"),
    SECURITY_FAIL_04(515, "enc or dec invoke fail !", "加解密接口调用失败"),
    SECURITY_FAIL_05(516, "enc or dec param is empty !", "需要加解密的数据集合不能为空"),
    SECURITY_FAIL_06(517, "enc or dec result is empty !", "加解密返回的数据为空"),
    ;

    // 唯一标识，必须全局唯一
    private final int code;
    // 英文描述
    private final String msg;
    // 中文描述
    private final String chineseDesc;

    public static BaseResultEnum getByCode(int code) {
        return Arrays.stream(BaseResultEnum.values()).filter(e -> e.getCode() == code).findFirst().orElse(SYSTEM_ERROR);
    }
}
